﻿using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace model2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainViewModel();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }

    public class UserModel : INotifyPropertyChanged
    {
        private string _name;
        private string _age;
        private string _email;

        public string Name
        {
            get => _name;
            set { _name = value; OnPropertyChanged(); }
        }

        public string Age
        {
            get => _age; 
            set
            {
                if (_age != value)
                {
                    // 校验：只能输入正整数
                    if (!int.TryParse(value, out int ageValue) || ageValue <= 0)
                    {
                        MessageBox.Show("年龄必须为正整数！", "输入错误", MessageBoxButton.OK, MessageBoxImage.Warning);
                        return;
                    }
                    _age = value;
                    OnPropertyChanged();
                }
            }
        }

        public string Email
        {
            get => _email;
            set { _email = value; OnPropertyChanged(); }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public class MainViewModel
    {
        public UserModel User { get; set; } = new UserModel();

        public ICommand ConfirmCommand { get; }

        public MainViewModel()
        {
            // 初始化命令，绑定到 OnConfirm 方法
            ConfirmCommand = new RelayCommand(_ => OnConfirm());
        }

        private void OnConfirm()
        {
            // 这里处理“确定”按钮的逻辑
            MessageBox.Show($"姓名: {User.Name}\n年龄: {User.Age}\n邮箱: {User.Email}", "提交信息");
        }
    }

    public class RelayCommand : ICommand
    {
        private readonly Action<object> _execute;
        private readonly Predicate<object> _canExecute;

        public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)
        {
            _execute = execute;
            _canExecute = canExecute;
        }

        public bool CanExecute(object parameter) => _canExecute == null || _canExecute(parameter);
        public void Execute(object parameter) => _execute(parameter);
        public event EventHandler CanExecuteChanged
        {
            add => CommandManager.RequerySuggested += value;
            remove => CommandManager.RequerySuggested -= value;
        }
    }
}